package com.pur.zsms.ordercustommanage.web;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.service.IFoodService;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.lockmanage.service.ILockTransService;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.po.ShoppingCartMaterPo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterTypeVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterVo;
import com.pur.zsms.note.Note;
import com.pur.zsms.note.NoteForSup;
import com.pur.zsms.note.service.INoteService;
import com.pur.zsms.ordercustommanage.po.OrderCustPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.service.ICustMaterDetailedOrderService;
import com.pur.zsms.ordercustommanage.service.ICustMaterOrderService;
import com.pur.zsms.ordercustommanage.service.ISelectFoodService;
import com.pur.zsms.ordercustommanage.service.InsertCusFoodService;
import com.pur.zsms.ordercustommanage.service.impl.CustMaterOrderServiceImpl;
import com.pur.zsms.ordercustommanage.vo.OrderCustFoodVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.OrderStateVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterRealVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVoFooter;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodList;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.ordercustommanage.vo.ExcelVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserLevelPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.quotation.service.IQuotationService;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.storemanage.vo.StoreInVo;
import com.pur.zsms.storemanage.vo.StoreMaterVo;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierUserPo;
import com.pur.zsms.supplier.service.ISupplierOrderService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.DeleteSupplierVo;
import com.pur.zsms.supplier.vo.DeleteSupplierVoList;
import com.pur.zsms.supplier.vo.SupplierUserVo;
import com.pur.zsms.timecontrol.service.ITimeControlService;
import com.pur.zsms.unusual.service.IUnusualService;
import com.pur.zsms.weixin.service.IWeixinService;

@Controller
@RequestMapping(value = "/selectMobile")
public class SelectMaterMobileControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SelectMaterMobileControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "weixinService")
	private IWeixinService weixinService;

	@Resource(name = "lockTransService")
	private ILockTransService lockTransService;

	@Resource(name = "quotationService")
	private IQuotationService quotationService;

	@Resource(name = "selectFoodService")
	private ISelectFoodService selectFoodService;

	@Resource(name = "insertCusFoodService")
	private InsertCusFoodService insertCusFoodService;

	@Resource(name = "custMaterOrderService")
	private ICustMaterOrderService custMaterOrderService;

	@Resource(name = "custMaterDetailedOrderService")
	private ICustMaterDetailedOrderService custMaterDetailedOrderService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "supplierOrderService")
	private ISupplierOrderService supplierOrderService;

	@Resource(name = "unusualService")
	private IUnusualService unusualService;

	@Resource(name = "storeService")
	private IStoreService storeService;

	@Resource(name = "timeControlService")
	private ITimeControlService timeControlService;

	@Resource(name = "materService")
	private IMaterService materService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@Resource(name = "noteService")
	private INoteService noteService;

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "foodService")
	private IFoodService foodService;

	@RequestMapping(value = "/initMaterType.do", method = { RequestMethod.POST })
	@ResponseBody
	public String initMaterType(MaterTypeVo materTypeVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());

		materTypeVo.setShopId(shopId);
		List<Object[]> listObject = materService.listSelectMaterTypeForMobile(materTypeVo);

		List<MaterTypeVo> listMaterVo = new ArrayList<MaterTypeVo>();

		if (listObject != null && listObject.size() > 0) {
			for (Object[] objects : listObject) {
				MaterTypeVo f = new MaterTypeVo();
				f.setMaterTypeName((String) objects[0]);
				f.setClassType((String) objects[1]);
				f.setSupIdAll((String) objects[2]);

				listMaterVo.add(f);
			}
		}

		json.put("materTypeListForinit", listMaterVo);
		json.put("success", true);
		return json.toString();
	}

	/***
	 * 
	 */
	@RequestMapping(value = "/initMater.do", method = { RequestMethod.POST })
	@ResponseBody
	public String initMater(MaterVo materVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());

		materVo.setCustId((int) shopId);
		List<Object[]> listObject = materService.listSelectMaterForMobile(materVo);

		List<MaterVo> listMaterVo = new ArrayList<MaterVo>();

		if (listObject != null && listObject.size() > 0) {
			for (Object[] objects : listObject) {
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) objects[0]);
				f.setMaterName((String) objects[1]);
				f.setMaterType((Integer) objects[2]);
				f.setMaterLevel((Integer) objects[3]);
				f.setMaterValue(((BigDecimal) objects[4]).multiply(new BigDecimal("1000")));
				f.setMaterProportion((Integer) objects[6]);
				f.setMid((Integer) objects[7]);
				f.setSupId((Integer) objects[8]);
				f.setSupName((String) objects[9]);
				// f.setMaterValueUndressed(((BigDecimal) item[10]).multiply(new
				// BigDecimal("1000")));
				f.setUnit((String) objects[11]);
				f.setSpecifications((String) objects[12]);
				f.setBrands((String) objects[13]);
				// 根据加价模式得到价钱
				BigDecimal materValueReal = supplierService.getMaterValueRealByMaterIdForLook(f.getMaterId(), f.getSupId(), shopId);
				f.setMaterValueUndressed(materValueReal);
				listMaterVo.add(f);
			}
		}

		json.put("materListForinit", listMaterVo);
		json.put("success", true);

		return json.toString();
	}

	@RequestMapping(value = "/addShoppingCartForInput.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addShoppingCartForInput(ShoppingCartMaterVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setCustId((int) shopId);

			boolean flag = materService.addShoppingCartForInput(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/addShoppingCartForAdd.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addShoppingCartForAdd(ShoppingCartMaterVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setCustId((int) shopId);

			boolean flag = materService.addShoppingCartForAdd(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/addShoppingCartForMin.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addShoppingCartForMin(ShoppingCartMaterVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setCustId((int) shopId);

			boolean flag = materService.addShoppingCartForMin(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/getShopCartNumForShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getShopCartNumForShop(ShoppingCartMaterVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setCustId((int) shopId);
			int num = materService.getShopCartNumForShop(c);
			json.put("num", num);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	/***
	 * 
	 */
	@RequestMapping(value = "/initShoppingMaterForMobile.do", method = { RequestMethod.POST })
	@ResponseBody
	public String initShoppingMaterForMobile(MaterVo materVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());

		materVo.setCustId((int) shopId);
		List<Object[]> listObject = materService.initShoppingMaterForMobile(materVo);

		List<MaterVo> listMaterVo = new ArrayList<MaterVo>();

		if (listObject != null && listObject.size() > 0) {
			for (Object[] objects : listObject) {
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) objects[0]);
				f.setMaterName((String) objects[1]);
				f.setMaterType((Integer) objects[2]);
				f.setMaterLevel((Integer) objects[3]);
				f.setMaterValue(((BigDecimal) objects[4]).multiply(new BigDecimal("1000")));
				f.setMaterProportion((Integer) objects[6]);
				f.setMid((Integer) objects[7]);
				f.setSupId((Integer) objects[8]);
				f.setSupName((String) objects[9]);
				// f.setMaterValueUndressed(((BigDecimal) item[10]).multiply(new
				// BigDecimal("1000")));
				f.setUnit((String) objects[11]);
				f.setSpecifications((String) objects[12]);
				f.setBrands((String) objects[13]);
				f.setMaterNum((BigDecimal) objects[14]);
				// 根据加价模式得到价钱
				BigDecimal materValueReal = supplierService.getMaterValueRealByMaterIdForLook(f.getMaterId(), f.getSupId(), shopId);
				f.setMaterValueUndressed(materValueReal);
				listMaterVo.add(f);
			}
		}

		json.put("materListForinit", listMaterVo);
		json.put("success", true);

		return json.toString();
	}
}
